package com.example.model.dict.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.model.dict.model.DictiModel;
import com.example.model.dict.service.DictiModelService;
import com.example.utils.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * @Classname DictiModelServiceImpl
 * @Description TODO
 * @Date 2022/8/2 21:01
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class DictiModelServiceImpl implements DictiModelService {

    @Autowired
    TableService tableService;

    @Override
    public DictiModel save(DictiModel dictiModel) {
        tableService.save(dictiModel,DictiModel.class);
        return dictiModel;
    }

    @Override
    public IPage<DictiModel> parentPage(int page, int pageSize, JSONObject object) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(DictiModel.class);
        sqlBuilder.and("pid").eq(0);
        if(object != null){
            Object dictname=object.get("dictname");
            if(!StringUtils.isEmpty(dictname)){
                sqlBuilder.and("dictname").like(dictname);
            }
            Object dictino=object.get("dictino");
            if (!StringUtils.isEmpty(dictino)) {
                sqlBuilder.and("dictino").like(dictino);
            }
            Object dictdesc=object.get("dictdesc");
            if (!StringUtils.isEmpty(dictdesc)) {
                sqlBuilder.and("dictdesc").like(dictdesc);
            }
        }
        sqlBuilder.page(page,pageSize);
        IPage<DictiModel> dictiModelIPage=tableService.getPage(sqlBuilder,DictiModel.class);
        return dictiModelIPage;
    }

    @Override
    public IPage<DictiModel> childPage(int page, int pageSize, Object pid, JSONObject object) {
        DictiModel dictiModel=getById(pid);
        if(dictiModel==null){
            return new IPage<>(0, Collections.emptyList());
        }
       SqlBuilder sqlBuilder = tableService.getSqlBuilder(DictiModel.class);
       sqlBuilder.and("pid").eq(pid);

       sqlBuilder.page(page, pageSize);
        IPage<DictiModel> dictiModelIPage=tableService.getPage(sqlBuilder,DictiModel.class);
        if(dictiModelIPage.getData().size() > 0){
            List<DictiModel> list=dictiModelIPage.getData();
            for (DictiModel dicti : list) {
                dicti.setDictname(dictiModel.getDictname());
            }
        }
        return dictiModelIPage;
    }

    @Override
    public DictiModel update(DictiModel dictiModel) {
        tableService.updateById(dictiModel,DictiModel.class);
        return dictiModel;
    }

    @Override
    public DictiModel getById(Object id) {
        DictiModel dictiModel=tableService.getOneById(id,DictiModel.class);
        return dictiModel;
    }

    @Override
    public DictiModel getDictiModelByDictiNo(String DictiNo) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(DictiModel.class);
        sqlBuilder.and("dictino").eq(DictiNo);
        DictiModel dictiModel=tableService.selectOne(sqlBuilder,DictiModel.class);
        return dictiModel;
    }
}
